/***
 * Contains basic SlickGrid formatters.
 * @module Formatters
 * @namespace Slick
 */

(function ($) {
    // register namespace
    $.extend(true, window, {
        "Slick": {
            "Formatters": {
                "PercentComplete": PercentCompleteFormatter,
                "PercentCompleteBar": PercentCompleteBarFormatter,
                "YesNo": YesNoFormatter,
                "Checkmark": CheckmarkFormatter,
                "DisableSupport": DisableSupportFormatter,
                "Enter": EnterFormatter,
                "ShowNDPrice": ShowNDPriceFormatter,
                "DocType": DocTypeFormatter,
                "ToDate": ToDateFormatter,
                "Image": ImageFormatter,
                "NDDiff": ndDifferenceFormatter,
                "EnterShowCustomer": EnterShowCustomerFormatter,
                "Status": DocStatusFormatter,
                "MeasurementFormatter": MeasurementFormatter,
                "CourseUnitFormatter": CourseUnitFormatter
            }
        }
    });

    function PercentCompleteFormatter(row, cell, value, columnDef, dataContext) {
        if (value == null || value === "") {
            return "-";
        } else if (value < 50) {
            return "<span style='color:red;font-weight:bold;'>" + value + "%</span>";
        } else {
            return "<span style='color:green'>" + value + "%</span>";
        }
    }

    function DisableSupportFormatter(row, cell, value, columnDef, dataContext) {
        if (value == 'N/A') {
            return "<div class='ui-state-disabled' style='text-align:center; background:gray;'><span style=' font-weight:bold; font-size:16px;'>" + '-' + "</span></div>";
        } else {
            var ndprice = dataContext.NDPrice;
            var ndpriceapprove1 = dataContext.NDPriceApprove1;
            var ndpriceapprove2 = dataContext.NDPriceApprove2;
            var ndpriceapprove3 = dataContext.NDPriceApprove3;
            var value2 = ndprice - value;

            //$('#approveLevelDetails').hide();
            if (value2 > ndpriceapprove1 && value2 <= ndpriceapprove2) {

                return "<div class='highlightPriceApporver1'><span >" + value + "</span></div>";
            }
            else if (value2 > ndpriceapprove2 && value2 <= ndpriceapprove3) {

                return "<div class='highlightPriceApporver2'><span >" + value + "</span></div>";
            }
            else if (value2 > ndpriceapprove3) {

                return "<div class='highlightPriceApporver3'><span >" + value + "</span></div>";
            }
            else {
                return value;
            }

        }
    }
    function ndDifferenceFormatter(row, cell, value, columnDef, dataContext) {
        var prevndprice = dataContext.olddiscountPrice;
        if (prevndprice != value) {
            return "<div class='highlightPriceApporver3'><span >" + value + "</span></div>";
        }
        else {
            return value;
        }

    }

    function EnterFormatter(row, cell, value, columnDef, dataContext) {
        var data = value;
        var isDraft = false;
        if (dataContext.Status === '30' || dataContext.Status === '20') {
            isDraft = true;
        }
        return '<a href="#" onclick="showLayout(\'3\',\'' + data + '\',\'' + dataContext.Doctype + '\',' + isDraft + ')">Enter</a>';

    }
    function EnterShowCustomerFormatter(row, cell, value, columnDef, dataContext) {
        return '<a href="#" onclick="EnterShowCustomer(\'' + value + '\')">Print</a>';
    }

    function ImageFormatter(row, cell, value, columnDef, dataContext) {
        var data = value;
        var isDraft = false;
        if (dataContext.Status === '30') {
            isDraft = true;
        }
        //alert(value);
        if (value != null) {
            return '<a href="#" onclick="showImage(\'' + dataContext.UserName + '\')">Show image</a>';
        }
        else {
            if (dataContext.RoleName != null && dataContext.RoleName == 'Approver') {

                return '<a href="#" onclick="showImage(\'' + dataContext.UserName + '\')">Add image</a>';
            }
        }

    }
    function ShowNDPriceFormatter(row, cell, value, columnDef, dataContext) {

        return '<a href="#" onclick="showNdPriceDetail(' + value + ')">Enter</a>';

    }

    function ToDateFormatter(row, cell, value, columnDef, dataContext) {

        return value;

    }
    function DocTypeFormatter(row, cell, value, columnDef, dataContext) {
        if (value == '01') {
            return 'Customer';
        } else if (value == '02') {
            return 'Price Group';
        } else {
            return 'N/A';
        }
    }

    function DocStatusFormatter(row, cell, value, columnDef, dataContext) {

        switch (value) {
            case '10':
                return 'Approved';
            case '11':
                return 'Waiting for approve';
            case '20':
                return 'Reject';
            case '30':
                return 'Draft';
            case '40':
                return 'Exported to SAP';
            case '50':
                return 'SAP uploaded';
            case '51':
                return 'Edited Data';

        }
    }
    function MeasurementFormatter(row, cell, value, columnDef, dataContext) {

        var objs = $.map(measurementData, function (obj, index) {
            return obj.Id === parseInt(value) ? obj : null;
        });
        return objs[0].Name ;
    }
    function CourseUnitFormatter(row, cell, value, columnDef, dataContext) {

        var objs = $.map(courseUnitData, function (obj, index) {
            return obj.ID === parseInt(value) ? obj : null;
        });
        return objs[0].Display;
    }
    function PercentCompleteBarFormatter(row, cell, value, columnDef, dataContext) {
        if (value == null || value === "") {
            return "";
        }

        var color;

        if (value < 30) {
            color = "red";
        } else if (value < 70) {
            color = "silver";
        } else {
            color = "green";
        }

        return "<span class='percent-complete-bar' style='background:" + color + ";width:" + value + "%'></span>";
    }

    function YesNoFormatter(row, cell, value, columnDef, dataContext) {
        return value ? "Yes" : "No";
    }

    function CheckmarkFormatter(row, cell, value, columnDef, dataContext) {
        return value ? "<img src='../images/tick.png'>" : "";
    }
})(jQuery);